﻿using Finbook.Entity;
using MassTransit;
using Order.API.Data;
using Order.API.Data.AggregatesModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Order.API.Services.Commands
{
    public class CreateOrderCmdHandler
        : IConsumer<CreateOrderCmd>
    {
        private readonly OrderContext _orderContext;
        public CreateOrderCmdHandler(OrderContext orderContext)
        {
            _orderContext = orderContext;
        }
        public async Task Consume(ConsumeContext<CreateOrderCmd> context)
        {
            await _orderContext.Orders.InsertOneAsync(new Data.AggregatesModel.Order()
            {
                Id = context.Message.Id,
                UserId = context.Message.UserId,
                CreateTime = context.Message.CreateTime
            });
            await Startup.BusControl.Publish(new OrderCreatedEvent()
            {
                Id = context.Message.Id,
                UserId = context.Message.UserId,
                CreateTime = context.Message.CreateTime
            });
        }
    }
}
